package com.aoxin.controller.course.webCourse;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aoxin.model.course.SlfMyCoursePage;
import com.aoxin.service.inter.course.webCourse.MyCoursePageService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.EmptyUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

@RestController
@RequestMapping("/web/myCoursePage/")
public class MyCoursePageController {

	@Autowired
	private MyCoursePageService myCoursePageService;

	/**
	 * 新增我的课程包
	 * 
	 * @param courseId
	 * @param res
	 * @return
	 */
	@LogInfo(content = "课程包", operateType = OperateType.INSERT)
	@RequestMapping("insertMyCoursePage")
	public AjaxRes insertMyCoursePage(Integer courseId, Integer syllabusId, Integer childrenId, AjaxRes res) {
		try {
		Integer rest=myCoursePageService.insertMyCoursePage(courseId, syllabusId, childrenId);
		if(rest==1){
			//普通购买
			res.setSucceedMsg(Const.PURCHASE_SUCCESS);
		}else if(rest==2){
			//尊享购买
			res.setSucceedMsg(Const.VIP_PURCHASE_SUCCESS);
		}
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
			e.printStackTrace();
		}

		return res;
	}

	/**
	 * 查询我的全部课程包|查询某个孩子课程包
	 * 
	 * @param slfMyCoursePage
	 *            ,res,pageInfo
	 * @return
	 */
	@LogInfo(content = "课程包", operateType = OperateType.QUERY)
	@RequestMapping("getMyCoursePage")
	public AjaxRes getMyCoursePage(SlfMyCoursePage slfMyCoursePage, AjaxRes res) {
		List<SlfMyCoursePage> MyCourses = myCoursePageService.getMyCoursePage(slfMyCoursePage);
		res.setSucceed(MyCourses, "getMyCourse success!");
		return res;
	}

	/**
	 * 删除我的课程包
	 * 
	 * @param myCoursePageId
	 * @param res
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.DELETE)
	@RequestMapping("deleteMyCoursePageByMyCourseId")
	public AjaxRes deleteMyCoursePageByMyCourseId(Integer myCoursePageId, AjaxRes res) {
		if (myCoursePageId != null) {
			Integer result = myCoursePageService.deleteMyCoursePage(myCoursePageId);
			if (result != 0) {
				res.setSucceed("deleteMyCourseByMyCourseId success!");
			} else {
				res.setFailMsg("deleteMyCourseByMyCourseId Fail!");
			}
		} else {
			res.setFailMsg("myCoursePageId is null");
		}
		return res;
	}

	/**
	 * 批量删除我的课程包
	 * 
	 * @param myCoursePageIds
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.DELETE)
	@RequestMapping("deleteMyCoursePageByMyCourseIds")
	public AjaxRes deleteMyCoursePageByMyCourseIds(@RequestParam("myCoursePageIds") List<Integer> myCoursePageIds, AjaxRes res) {
		if (myCoursePageIds != null && myCoursePageIds.size() > 0) {
			Integer result = myCoursePageService.deleteMyCoursePages(myCoursePageIds);
			if (result != 0) {
				res.setSucceed("deleteMyCourseByMyCourseIds success!");
			} else {
				res.setFailMsg("deleteMyCourseByMyCourseIds Fail!");
			}
		} else {
			res.setFailMsg("myCoursePageIds is null");
		}
		return res;
	}

	/**
	 * 修改我的课程包
	 * 
	 * @param slfMyCoursePage
	 * @param res
	 * @return
	 */
	@LogInfo(content = "课程", operateType = OperateType.UPDATE)
	@RequestMapping("updateMyCoursePage")
	public AjaxRes updateMyCoursePage(SlfMyCoursePage slfMyCoursePage, AjaxRes res) {
		try {
			if (!EmptyUtil.objFieldsIsEmpty(slfMyCoursePage)) {
				Integer result = myCoursePageService.updateMyCoursePage(slfMyCoursePage);
				if (result != 0) {
					res.setSucceed("updateMyCourse success!");
				} else {
					res.setFailMsg("updateMyCourse Fail!");
				}
			} else {
				res.setFailMsg("slfMyCoursePage is null");
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return res;
	}

	// TODO:新增消费记录
	// TODO:查询余额
	
	@LogInfo(content = "查询是否购买上级包", operateType = OperateType.QUERY)
	@RequestMapping("queryUserIsBuy")
	public AjaxRes queryUserIsBuy(Integer childrenId, Integer schoolCoursePageId, AjaxRes res) {
		//true 可购买 false不可购买
		boolean result = myCoursePageService.queryUserIsBuy(childrenId,schoolCoursePageId);
		res.setSucceed(result, Const.DATA_SUCCEED);
		return res;
	}
	
	@LogInfo(content = "查询上级包id", operateType = OperateType.QUERY)
	@RequestMapping("querySchoolCoursePageId")
	public AjaxRes querySchoolCoursePageId(Integer coursePageId, AjaxRes res) {
		Integer result = myCoursePageService.querySchoolCoursePageId(coursePageId);
		res.setSucceed(result, Const.DATA_SUCCEED);
		return res;
	}
	
	
}
